﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _5._19EmpireRecordsApp.Data.Model;
using _5._19EmpireRecordsApp.Data;


namespace _5._19EmpireRecordsApp.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            List<IndexArtistViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Artist.Select(x => new IndexArtistViewModel()
                    {
                        id = x.id,
                        MusicianName = x.ArtistName,
                        Type = x.Genre

                    }).ToList();
                

            }
            return View(model);
        }
        public ActionResult ViewAlbums()
        {
            List<IndexAlbumViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Album.Select(x => new IndexAlbumViewModel()
                {
                    Id = x.ID,
                    CDName = x.AlbumName,
                    Kind = x.EPLP

                }).ToList();

            }
            return View(model);
        }

        public ActionResult AddAlbum(int id)
        {
        
            ViewBag.Message = "Add Albums to the database.";
            Album SpecificAlbum = new Album();
            SpecificAlbum.Artist_AlbumID = id;
            return View(SpecificAlbum);
        }
        [HttpPost]
        public ActionResult AddAlbum(Album model, int id)
        {
            model.Artist_AlbumID = id;
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
            
                    db.Album.Add(model);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");

            //just for checkin
        }

        public ActionResult DeleteArtist(int? ID)
        {
            if (ID != null)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    Artist model = db.Artist.FirstOrDefault(x => x.id == ID);
                  
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");


        }
        public ActionResult DeleteAlbum(int Id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
           
            {
                Album model = db.Album.FirstOrDefault(x=> x.ID == Id);
                db.Album.Remove(model);
                db.SaveChanges();
            }
            return RedirectToAction("ViewAlbums"); 
        }
   
        public ActionResult ViewSingleAlbum(int ID)
        {
           
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Album model = db.Album.FirstOrDefault(x => x.ID == ID);
                return View(model);
            }
           
        }
    
        public ActionResult AddArtist()
        {
            ViewBag.Message = "Add Artists to the database.";
            Artist SpecificArtist = new Artist();
           
            
            return View(SpecificArtist);
           
        }
        [HttpPost]
        public ActionResult AddArtist(Artist model)
        {
         
            if (ModelState.IsValid)
             
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {

             //       var idnumber = model.id;
             //       model.Artist_AlbumID.Equals(idnumber);
                    db.Artist.Add(model);
                    db.SaveChanges();
                  
                
                    
                }
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    var FlyUpdate = db.Artist.FirstOrDefault(x => x.id == model.id);
                    FlyUpdate.Artist_AlbumID = model.id;
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");


        }
        public ActionResult EditArtist(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                return View(db.Artist.FirstOrDefault(x => x.id == id));
            }
        }
        [HttpPost]
        public ActionResult EditArtist(int id, string ArtistName, Genre Genre)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var ChangedArtist = db.Artist.FirstOrDefault(x => x.id == id);
                ChangedArtist.id = id;
                ChangedArtist.ArtistName = ArtistName;
                ChangedArtist.Genre = Genre;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

        }

        public ActionResult EditAlbumArtist(int ID, int Artist_AlbumID, string AlbumName, EPLP EPLP)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var NewAblumArtist = new Artist_Album();

                return View(NewAblumArtist);
            }
        }
        [HttpPost]
        public ActionResult EditAlbumArtist(Artist_Album model, int ID, int Artist_AlbumID, string AlbumName, EPLP EPLP )
        {
          
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
         

    // watch this needs to pull the new Artist_AlbumID and reassign it to the album
                // NewAblumArtist.Albums.Artist_AlbumID = Artist_AlbumID;
                model.Albums.ID = ID;
                model.Albums.AlbumName = AlbumName;
                model.Albums.EPLP = EPLP;
                model.Albums.Artist_AlbumID = Artist_AlbumID;
                db.Album_Artist.Add(model);
                db.SaveChanges();
        
            }
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var FlyUpdate = db.Artist.FirstOrDefault(x => x.id == model.Artists.id);
                FlyUpdate.Artist_AlbumID = model.Artists.id;
                db.SaveChanges();
            }
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Album DeleteOldAlbum = db.Album.FirstOrDefault(x => x.ID == ID);
                db.Album.Remove(DeleteOldAlbum);
                db.SaveChanges();
            }
            using (ApplicationDbContext db  = new ApplicationDbContext())
            {
                var AlbumUpdate = db.Album.FirstOrDefault(x => x.ID == model.Albums.ID);
                AlbumUpdate.Artist_AlbumID = model.Artists.id;
                db.SaveChanges();
            }
            
            return RedirectToAction("Index");
        }
        public ActionResult ViewAlbumByArtist(int id)
        {
             List<Album> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
        model = db.Album.Where(x => x.Artist_AlbumID == id).ToList();
            }
             return View (model);
        }
      
       
    }
}